﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eBlog.Common;
using eBlog.Common.Files;
using eBlog.Common.Security;
using eBlog.Core;
using eBlog.Entity;
using DBScript;
namespace eBlog.Web.Users
{
    public partial class Login : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
          //  IsLogin();//判断是否登录

            string model = Request.QueryString["action"];

            if (!string.IsNullOrEmpty(model))//来自表单的请求
            {
                if (model == "login")
                {
                    LoginUser();
                }
                if(model=="quit")//注销
                {
                    HttpCookie UserCookie = new HttpCookie("User");
                    UserCookie.Expires = DateTime.Now.AddMinutes(-30);
                    Response.Cookies.Add(UserCookie);

                    Response.Redirect("/");
                    return;
                }

            }
            else//不是表单的请求
            {
                Init_Template();
                TemplateUrl = "/Users/login.html";

                this.WriteHTML();
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        protected void LoginUser()
        {

            string emailusername = PageValidate.Form("User[email]");
            string password = PageValidate.Form("User[password]");
            //string autologin = PageValidate.Form("autologin");

            string Err = "";
            if (emailusername == "" || password == "")
            {
                Err += "用户名或密码必须输入!";
                MessageBox.Show(Err);
                return;
            }

           IDBBase dal = DBFactory.GetDbProvider();
           User model = dal.From<User>().Where("(LoginName=@UserName or Email=@UserName)")
                 .SetString("UserName", emailusername)
                 .GetModel<User>();

            if (model != null)
            {

                if (model.PassWord == MD5.ADD(password))
                {

                    model.PassWord = MD5.ADD(password);
                   // model.LastLoginIP = Request.UserHostAddress;
                    model.LastLoginTime = DateTime.Now;
                    dal.Update<User>(model);
                    HttpCookie UserCookie = new HttpCookie("User");
                    UserCookie["id"] = model.ID.ToString();
                    UserCookie["name"] = Server.UrlEncode(model.LoginName);
                    UserCookie["key"] = MD5.FormatCookie(model.LoginName);

                    //if (autologin != "")
                    //{
                    //    UserCookie.Expires = DateTime.Now.AddMinutes(30);
                    //}
                    Response.Cookies.Add(UserCookie);
                    
                    Response.Redirect(ConfigHelper.SiteUrl);

                }
                else
                {
                    MessageBox.Show("用户名/邮箱或密码错误!");
                    return;
                }
            }
            else
            {
                MessageBox.Show("用户名/邮箱或密码错误!");
                return;

            }


        }

        /// <summary>
        /// 判断是否登录
        /// </summary>
        protected void IsLogin()
        {
            HttpCookie cookie = Request.Cookies["User"];


            if (cookie != null)
            {
                Response.Redirect(ConfigHelper.SiteUrl);
            }

        }
    }
}